﻿// <copyright file="HandleFormComboBox.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-01-29</date>
// <summary>HandleFormComboBox class definition</summary>

namespace Microsoft.WebAnalytics.Behaviors.FormTrackers
{
    using System;
    using System.Windows.Controls;
    using Microsoft.WebAnalytics.Contracts;

    /// <summary>
    /// Handle the ComboBox SelectionChanged event for a form control
    /// </summary>
    [FormTracker(typeof(ComboBox))]
    public class HandleFormComboBox : ITrackFormControl
    {
        #region Fields
        /// <summary>
        /// The event handler to track against
        /// </summary>
        private EventHandler<FormEventArgs> handler;
        #endregion

        #region ITrackFormControl Members

        /// <summary>
        /// Attach to the ComboBox SelectionChanged event
        /// </summary>
        /// <param name="control">a ComboBox</param>
        /// <param name="eventHandler">the Event handler to dispatch to</param>
        public void AttachEvents(Control control, EventHandler<FormEventArgs> eventHandler)
        {
            System.Diagnostics.Debug.Assert(control != null, "control cannot be null.");

            System.Diagnostics.Debug.Assert(eventHandler != null, "handler cannot be null.");

            this.handler = eventHandler;

            var combo = control as ComboBox;

            combo.SelectionChanged += new SelectionChangedEventHandler(this.SelectionChanged);
        }

        /// <summary>
        /// Selection Changed event handler
        /// </summary>
        /// <param name="sender">the ComboBox</param>
        /// <param name="e">the selection changed event arguments</param>
        private void SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var formEvent = new FormEventArgs 
            { 
                Control = sender as Control,
                EventName = "SelectionChanged",
                InnerEvent = e 
            };

            this.handler(this, formEvent);
        }

        #endregion
    }
}
